package bb.lanxing.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.lang.ref.WeakReference;

import bb.lanxing.activity.base.WebActivity;
import bb.lanxing.util.text.TextUtils;

public class WebViewBuilder {
    private WeakReference<Context> contextRef;
    private String title;
    private String url;

    private WebViewBuilder() {
    }

    public static WebViewBuilder with(Context context) {
        return new WebViewBuilder().context(context);
    }

    private WebViewBuilder context(Context context) {
        this.contextRef = new WeakReference<>(context);
        return this;
    }

    public WebViewBuilder title(String str) {
        this.title = str;
        return this;
    }

    public WebViewBuilder url(String str) {
        this.url = str;
        return this;
    }

    protected Intent newIntent(Context context) {
        return new Intent(context, WebActivity.class);
    }

    public void start() {
        WeakReference<Context> weakReference = this.contextRef;
        Context context = weakReference != null ? weakReference.get() : null;
        if (context != null && !TextUtils.isEmptyOrNull(this.url)) {
            Intent newIntent = newIntent(context);
            if (!TextUtils.isEmptyOrNull(this.title)) {
                newIntent.putExtra("title", this.title);
            }
            newIntent.putExtra("web_url", this.url);
            if (!(context instanceof Activity)) {
                newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(newIntent);
        }
    }
}
